package com.easyattendance.nls;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class UseProperties {

	public static Properties loadProperties(String fileName) {
		/*ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		URL url = classLoader.getResource("/Attendance/src/com/attedance/nls/Attendance.properties");
		File file;*/
		Properties tempProp = new Properties();
		try {
			//file = new File(url.toURI());
		
		InputStream inPropFile = UseProperties.class.getResourceAsStream("./Attendance.properties");
		//File file = new File("/Attendance/src/com/attedance/nls/Attendance.properties");
		
		//FileInputStream inPropFile;
		
			//inPropFile = new FileInputStream(file);
			tempProp.load(inPropFile);
			inPropFile.close();
			
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*catch (URISyntaxException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}*/
		return tempProp;
	}

	
	public static Properties incrementSectionIdCounterValue(Properties p) {

		Properties newProps = new Properties();
		Set enumProps = p.entrySet();
		String key = "";
		Iterator itr = enumProps.iterator();
		
		while ( itr.hasNext() ) {
			 Map.Entry mapEntry = (Map.Entry) itr.next();
			//key = (String) enumProps.nextElement();
			 if (mapEntry.getKey().equals("easyattendance.label.sectionidcounter")) {
				 int counter = Integer.parseInt((String) mapEntry.getValue());
				 counter++;
			    newProps.setProperty((String) mapEntry.getKey(), counter+"");
			    storeFile(newProps);
			 }
			 else {
				 newProps.setProperty((String) mapEntry.getKey(), p.getProperty((String) mapEntry.getKey()));
				 storeFile(newProps);
			 }
		}
		return newProps;
	}
	
	public static Properties decrementSectionIdCounterValue(Properties p) {

		Properties newProps = new Properties();
		Set enumProps = p.entrySet();
		String key = "";
		Iterator itr = enumProps.iterator();
		
		while ( itr.hasNext() ) {
			 Map.Entry mapEntry = (Map.Entry) itr.next();
			//key = (String) enumProps.nextElement();
			 if (mapEntry.getKey().equals("easyattendance.label.sectionidcounter")) {
				 int counter = Integer.parseInt((String) mapEntry.getValue());
				 counter--;
			    newProps.setProperty((String) mapEntry.getKey(), counter+"");
			    storeFile(newProps);
			 }
			 else {
				 newProps.setProperty((String) mapEntry.getKey(), p.getProperty((String) mapEntry.getKey()));
				 storeFile(newProps);
			 }
		}
		return newProps;
	}
	
	public static Properties incrementMaterialIdCounterValue(Properties p) {

		Properties newProps = new Properties();
		Set enumProps = p.entrySet();
		String key = "";
		Iterator itr = enumProps.iterator();
		
		while ( itr.hasNext() ) {
			 Map.Entry mapEntry = (Map.Entry) itr.next();
			//key = (String) enumProps.nextElement();
			 if (mapEntry.getKey().equals("easyattendance.label.materialidcounter")) {
				 int counter = Integer.parseInt((String) mapEntry.getValue());
				 counter++;
			    newProps.setProperty((String) mapEntry.getKey(), counter+"");
			    storeFile(newProps);
			 }
			 else {
				 newProps.setProperty((String) mapEntry.getKey(), p.getProperty((String) mapEntry.getKey()));
				 storeFile(newProps);
			 }
		}
		return newProps;
	}
	
	public static Properties decrementMaterialIdCounterValue(Properties p) {

		Properties newProps = new Properties();
		Set enumProps = p.entrySet();
		String key = "";
		Iterator itr = enumProps.iterator();
		
		while ( itr.hasNext() ) {
			 Map.Entry mapEntry = (Map.Entry) itr.next();
			//key = (String) enumProps.nextElement();
			 if (mapEntry.getKey().equals("easyattendance.label.materialidcounter")) {
				 int counter = Integer.parseInt((String) mapEntry.getValue());
				 counter--;
			    newProps.setProperty((String) mapEntry.getKey(), counter+"");
			    storeFile(newProps);
			 }
			 else {
				 newProps.setProperty((String) mapEntry.getKey(), p.getProperty((String) mapEntry.getKey()));
				 storeFile(newProps);
			 }
		}
		return newProps;
	}	
	
	public static Properties incrementAssignmentIdCounterValue(Properties p) {

		Properties newProps = new Properties();
		Set enumProps = p.entrySet();
		String key = "";
		Iterator itr = enumProps.iterator();
		
		while ( itr.hasNext() ) {
			 Map.Entry mapEntry = (Map.Entry) itr.next();
			//key = (String) enumProps.nextElement();
			 if (mapEntry.getKey().equals("easyattendance.label.assgnidcounter")) {
				 int counter = Integer.parseInt((String) mapEntry.getValue());
				 counter++;
			    newProps.setProperty((String) mapEntry.getKey(), counter+"");
			    storeFile(newProps);
			 }
			 else {
				 newProps.setProperty((String) mapEntry.getKey(), p.getProperty((String) mapEntry.getKey()));
				 storeFile(newProps);
			 }
		}
		return newProps;
	}
	
	public static Properties decrementAssignmentIdCounterValue(Properties p) {

		Properties newProps = new Properties();
		Set enumProps = p.entrySet();
		String key = "";
		Iterator itr = enumProps.iterator();
		
		while ( itr.hasNext() ) {
			 Map.Entry mapEntry = (Map.Entry) itr.next();
			//key = (String) enumProps.nextElement();
			 if (mapEntry.getKey().equals("easyattendance.label.assgnidcounter")) {
				 int counter = Integer.parseInt((String) mapEntry.getValue());
				 counter--;
			    newProps.setProperty((String) mapEntry.getKey(), counter+"");
			    storeFile(newProps);
			 }
			 else {
				 newProps.setProperty((String) mapEntry.getKey(), p.getProperty((String) mapEntry.getKey()));
				 storeFile(newProps);
			 }
		}
		return newProps;
	}	
	
	public static Properties incrementParentIdCounterValue(Properties p) {

		Properties newProps = new Properties();
		Set enumProps = p.entrySet();
		String key = "";
		Iterator itr = enumProps.iterator();
		
		while ( itr.hasNext() ) {
			 Map.Entry mapEntry = (Map.Entry) itr.next();
			//key = (String) enumProps.nextElement();
			 if (mapEntry.getKey().equals("easyattendance.label.parentidcounter")) {
				 int counter = Integer.parseInt((String) mapEntry.getValue());
				 counter++;
			    newProps.setProperty((String) mapEntry.getKey(), counter+"");
			    storeFile(newProps);
			 }
			 else {
				 newProps.setProperty((String) mapEntry.getKey(), p.getProperty((String) mapEntry.getKey()));
				 storeFile(newProps);
			 }
		}
		return newProps;
	}
	
	public static Properties decrementParentIdCounterValue(Properties p) {

		Properties newProps = new Properties();
		Set enumProps = p.entrySet();
		String key = "";
		Iterator itr = enumProps.iterator();
		
		while ( itr.hasNext() ) {
			 Map.Entry mapEntry = (Map.Entry) itr.next();
			//key = (String) enumProps.nextElement();
			 if (mapEntry.getKey().equals("easyattendance.label.parentidcounter")) {
				 int counter = Integer.parseInt((String) mapEntry.getValue());
				 counter--;
			    newProps.setProperty((String) mapEntry.getKey(), counter+"");
			    storeFile(newProps);
			 }
			 else {
				 newProps.setProperty((String) mapEntry.getKey(), p.getProperty((String) mapEntry.getKey()));
				 storeFile(newProps);
			 }
		}
		return newProps;
	}
	
	public static Properties incrementTimeSlotIdCounterValue(Properties p) {

		Properties newProps = new Properties();
		Set enumProps = p.entrySet();
		String key = "";
		Iterator itr = enumProps.iterator();
		
		while ( itr.hasNext() ) {
			 Map.Entry mapEntry = (Map.Entry) itr.next();
			//key = (String) enumProps.nextElement();
			 if (mapEntry.getKey().equals("easyattendance.label.timeslotidcounter")) {
				 int counter = Integer.parseInt((String) mapEntry.getValue());
				 counter++;
			    newProps.setProperty((String) mapEntry.getKey(), counter+"");
			    storeFile(newProps);
			 }
			 else {
				 newProps.setProperty((String) mapEntry.getKey(), p.getProperty((String) mapEntry.getKey()));
				 storeFile(newProps);
			 }
		}
		return newProps;
	}

	public static Properties incrementSubjectIdCounterValue(Properties p) {

		Properties newProps = new Properties();
		Set enumProps = p.entrySet();
		String key = "";
		Iterator itr = enumProps.iterator();
		
		while ( itr.hasNext() ) {
			 Map.Entry mapEntry = (Map.Entry) itr.next();
			//key = (String) enumProps.nextElement();
			 if (mapEntry.getKey().equals("easyattendance.label.subjectidcounter")) {
				 int counter = Integer.parseInt((String) mapEntry.getValue());
				 counter++;
			    newProps.setProperty((String) mapEntry.getKey(), counter+"");
			    storeFile(newProps);
			 }
			 else {
				 newProps.setProperty((String) mapEntry.getKey(), p.getProperty((String) mapEntry.getKey()));
				 storeFile(newProps);
			 }
		}
		return newProps;
	}
	
	public static Properties decrementSubjectIdCounterValue(Properties p) {

		Properties newProps = new Properties();
		Set enumProps = p.entrySet();
		String key = "";
		Iterator itr = enumProps.iterator();
		
		while ( itr.hasNext() ) {
			 Map.Entry mapEntry = (Map.Entry) itr.next();
			//key = (String) enumProps.nextElement();
			 if (mapEntry.getKey().equals("easyattendance.label.subjectidcounter")) {
				 int counter = Integer.parseInt((String) mapEntry.getValue());
				 counter--;
			    newProps.setProperty((String) mapEntry.getKey(), counter+"");
			    storeFile(newProps);
			 }
			 else {
				 newProps.setProperty((String) mapEntry.getKey(), p.getProperty((String) mapEntry.getKey()));
				 storeFile(newProps);
			 }
		}
		return newProps;
	}	
	
	public static Properties incrementStudentIdCounterValue(Properties p) {

		Properties newProps = new Properties();
		Set enumProps = p.entrySet();
		String key = "";
		Iterator itr = enumProps.iterator();
		
		while ( itr.hasNext() ) {
			 Map.Entry mapEntry = (Map.Entry) itr.next();
			//key = (String) enumProps.nextElement();
			 if (mapEntry.getKey().equals("easyattendance.label.studentidcounter")) {
				 int counter = Integer.parseInt((String) mapEntry.getValue());
				 counter++;
			    newProps.setProperty((String) mapEntry.getKey(), counter+"");
			    storeFile(newProps);
			 }
			 else {
				 newProps.setProperty((String) mapEntry.getKey(), p.getProperty((String) mapEntry.getKey()));
				 storeFile(newProps);
			 }
		}
		return newProps;
	}
	
	public static Properties decrementStudentIdCounterValue(Properties p) {

		Properties newProps = new Properties();
		Set enumProps = p.entrySet();
		String key = "";
		Iterator itr = enumProps.iterator();
		
		while ( itr.hasNext() ) {
			 Map.Entry mapEntry = (Map.Entry) itr.next();
			//key = (String) enumProps.nextElement();
			 if (mapEntry.getKey().equals("easyattendance.label.studentidcounter")) {
				 int counter = Integer.parseInt((String) mapEntry.getValue());
				 counter--;
			    newProps.setProperty((String) mapEntry.getKey(), counter+"");
			    storeFile(newProps);
			 }
			 else {
				 newProps.setProperty((String) mapEntry.getKey(), p.getProperty((String) mapEntry.getKey()));
				 storeFile(newProps);
			 }
		}
		return newProps;
	}
	
	public static Properties incrementTeacherIdCounterValue(Properties p) {

		Properties newProps = new Properties();
		Set enumProps = p.entrySet();
		String key = "";
		Iterator itr = enumProps.iterator();
		
		while ( itr.hasNext() ) {
			 Map.Entry mapEntry = (Map.Entry) itr.next();
			//key = (String) enumProps.nextElement();
			 if (mapEntry.getKey().equals("easyattendance.label.teacheridcounter")) {
				 int counter = Integer.parseInt((String) mapEntry.getValue());
				 counter++;
			    newProps.setProperty((String) mapEntry.getKey(), counter+"");
			    storeFile(newProps);
			 }
			 else {
				 newProps.setProperty((String) mapEntry.getKey(), p.getProperty((String) mapEntry.getKey()));
				 storeFile(newProps);
			 }
		}
		return newProps;
	}	
	
	public static Properties decrementTeacherIdCounterValue(Properties p) {

		Properties newProps = new Properties();
		Set enumProps = p.entrySet();
		String key = "";
		Iterator itr = enumProps.iterator();
		
		while ( itr.hasNext() ) {
			 Map.Entry mapEntry = (Map.Entry) itr.next();
			//key = (String) enumProps.nextElement();
			 if (mapEntry.getKey().equals("easyattendance.label.teacheridcounter")) {
				 int counter = Integer.parseInt((String) mapEntry.getValue());
				 counter--;
			    newProps.setProperty((String) mapEntry.getKey(), counter+"");
			    storeFile(newProps);
			 }
			 else {
				 newProps.setProperty((String) mapEntry.getKey(), p.getProperty((String) mapEntry.getKey()));
				 storeFile(newProps);
			 }
		}
		return newProps;
	}	
	
	
	public static Properties incrementClassIdCounterValue(Properties p) {

		Properties newProps = new Properties();
		Set enumProps = p.entrySet();
		String key = "";
		Iterator itr = enumProps.iterator();
		
		while ( itr.hasNext() ) {
			 Map.Entry mapEntry = (Map.Entry) itr.next();
			//key = (String) enumProps.nextElement();
			 if (mapEntry.getKey().equals("easyattendance.label.classidcounter")) {
				 int counter = Integer.parseInt((String) mapEntry.getValue());
				 counter++;
			    newProps.setProperty((String) mapEntry.getKey(), counter+"");
			    storeFile(newProps);
			 }
			 else {
				 newProps.setProperty((String) mapEntry.getKey(), p.getProperty((String) mapEntry.getKey()));
				 storeFile(newProps);
			 }
		}
		return newProps;
	}
	
	/*public static Properties incrementYearIdCounterValue(Properties p) {

		Properties newProps = new Properties();
		Set enumProps = p.entrySet();
		String key = "";
		Iterator itr = enumProps.iterator();
		
		while ( itr.hasNext() ) {
			 Map.Entry mapEntry = (Map.Entry) itr.next();
			//key = (String) enumProps.nextElement();
			 if (mapEntry.getKey().equals("easyattendance.label.yearidcounter")) {
				 int counter = Integer.parseInt((String) mapEntry.getValue());
				 counter++;
			    newProps.setProperty((String) mapEntry.getKey(), counter+"");
			    storeFile(newProps);
			 }
			 else {
				 newProps.setProperty((String) mapEntry.getKey(), p.getProperty((String) mapEntry.getKey()));
				 storeFile(newProps);
			 }
		}
		return newProps;
	} */
	
	
		
	public static void storeFile(Properties newProps) {
		OutputStream outPropFile;
		//ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		URL url = UseProperties.class.getResource("./Attendance.properties");
		
		try {
			File file = new File(url.toURI());
			outPropFile = new FileOutputStream(file);
			newProps.store(outPropFile, "Properties File modified");
			outPropFile.close();
		}
		 
		 catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		
	}
	public static String getSectionIdCounter(Properties p) {

		return (String) p.get("easyattendance.label.sectionidcounter");
	}
	
	public static String getSectionIdPrefix(Properties p) {

		return (String) p.get("easyattendance.label.sectionidprefix");
	}
	
	public static String getMaterialIdCounter(Properties p) {

		return (String) p.get("easyattendance.label.materialidcounter");
	}
	
	public static String getMaterialIdPrefix(Properties p) {

		return (String) p.get("easyattendance.label.materialidprefix");
	}
	
	public static String getAssignmentIdCounter(Properties p) {

		return (String) p.get("easyattendance.label.assgnidcounter");
	}
	
	public static String getAssignmentIdPrefix(Properties p) {

		return (String) p.get("easyattendance.label.assgnidprefix");
	}
	
	public static String getTeacherIdCounter(Properties p) {

		return (String) p.get("easyattendance.label.teacheridcounter");
	}
	
	public static String getTeacherIdPrefix(Properties p) {

		return (String) p.get("easyattendance.label.teacheridprefix");
	}
	
	public static String getClassIdCounter(Properties p) {

		return (String) p.get("easyattendance.label.classidcounter");
	}
	
	public static String getClassIdPrefix(Properties p) {

		return (String) p.get("easyattendance.label.classidprefix");
	}
	
	/* public static String getYearIdCounter(Properties p) {

		return (String) p.get("easyattendance.label.yearidcounter");
	}
	
	public static String getYearIdPrefix(Properties p) {

		return (String) p.get("easyattendance.label.yearidprefix");
	}	*/
	
	public static String getSubjectIdPrefix(Properties p) {

		return (String) p.get("easyattendance.label.subjectidprefix");
	}
	
	public static String getSubjectIdCounter(Properties p) {

		return (String) p.get("easyattendance.label.subjectidcounter");
	}
	
	/*public static String getStudentIdPrefix(Properties p) {

		return (String) p.get("easyattendance.label.studentidprefix");
	}*/
	
	public static String getStudentIdCounter(Properties p) {

		return (String) p.get("easyattendance.label.studentidcounter");
	}
	
	public static String getTimeSlotIdPrefix(Properties p) {

		return (String) p.get("easyattendance.label.timeslotidprefix");
	}
	
	public static String getTimeSlotIdCounter(Properties p) {

		return (String) p.get("easyattendance.label.timeslotidcounter");
	}
	
	public static String getParentIdPrefix(Properties p) {

		return (String) p.get("easyattendance.label.parentidprefix");
	}
	
	public static String getParentIdCounter(Properties p) {

		return (String) p.get("easyattendance.label.parentidcounter");
	}

}
